<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  title="标本处理"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateConfirmModel"
  size="small"

)
  mt-form(
      v-bind="editFormProps"
      style="margin-top: 10px;"
    )
</template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import {
  type FormItemProps,
  useNotify,
  type EditFormProps,
} from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Array as PropType<SpecimenEntity[]>,
    default: () => [],
  },
  batchNumber: {
    type: String,
    default: () => "",
  },
});
const $api = useApi();
const $notify = useNotify();
const editingObj = ref<{} | Object>({});

const appmodel = defineModel({
  default: false,
});
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const { request, loadingMap } = useRequest({
  save: (params: any) => {
    return $api.lis.specimen.batchSpecimenProcessing(params).then(() => {
      $notify.success("处理成功");
      emit("message-to-parent");
      appmodel.value = false;
    });
  },
});

const updateConfirmModel = () => {
  if (!editingObj?.value?.handlingMethod) {
    $notify.error("请选择处理方式");
    return;
  }
  request("save", {
    specimenList: props.selectionRow,
    handlingMethod: editingObj?.value?.handlingMethod,
    batchNumber: props.batchNumber,
  });
};

const editFormProps = computed<EditFormProps>(() => {
  const formItems: FormItemProps[] = [
    {
      prop: "handlingMethod",
      label: "处理方式:",
      required: true,
      as: "select",
      options: {
        loadMethod: () => {
          return [
            {
              label: "冷冻保存",
              value: "冷冻保存",
            },
            {
              label: "常温或冷藏保存",
              value: "常温或冷藏保存",
            },
            {
              label: "进一步检测",
              value: "进一步检测",
            },
            {
              label: "胚胎处理",
              value: "胚胎处理",
            },
            {
              label: "销毁",
              value: "销毁",
            },
          ];
        },
      },
    },
  ];

  const editProps: EditFormProps = {
    items: formItems,
    model: editingObj.value,
    columns: 1,
  };
  return editProps;
});

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      editingObj.value = {};
    }
  },
  {
    immediate: true,
  }
);
</script>
